<?php
require_once "config.php";
require_once "GoogleAPI.php";
$googleAPI = new GoogleAPI();

$json = $googleAPI->getBlogResults(@$_REQUEST['start'],@$_REQUEST['q']);

$next_start = '';
$previous_start = '';
$currentPageIndex = $json->responseData->cursor->currentPageIndex;

if (isset($json->responseData->cursor->pages[$currentPageIndex+1])) {
	$next_start = $json->responseData->cursor->pages[$currentPageIndex+1]->start;
    $query = $_REQUEST['q'];
	
}

if (isset($json->responseData->cursor->pages[$currentPageIndex-1])) {
	$previous_start = $json->responseData->cursor->pages[$currentPageIndex-1]->start;
    $query = $_REQUEST['q'];
}


$results = $json->responseData->results;
foreach ($results as $key=>$data) {
    echo '<h2><a href="'.$data->postUrl.'" target="_blank">'.$data->title.'</a></h2>';
    echo '<p>'.strip_tags($data->content).'</p>';
}

echo '<div class="alignCenter" id="more_result_button">';
if ($next_start != '') {
    echo '<span class="green" onclick="gotoNextPage(\''.$next_start.'\',\''.$query.'\')" style="cursor:pointer">更多結果</span><span id="next_loading"></span>';
} else {
    echo '<span>無更多結果</span>';
}
echo '</div>';

?>

